{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# LLM类大模型的流式输出方法\n",
    "\n",
    "from langchain.llms import OpenAI\n",
    "\n",
    "import os \n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-zk2f3fc211ff0ad224ecd79efe339d0861d66379d4d11320\"\n",
    "os.environ[\"OPENAI_PROXY\"] = \"https://flag.smarttrot.com/v1/\"\n",
    "\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 构造一个LLM\n",
    "\n",
    "llm = OpenAI(\n",
    "    model = \"gpt-3.5-turbo-instruct\",\n",
    "    temperature = 0.5,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base,\n",
    "    max_tokens = 500\n",
    ")\n",
    "\n",
    "for chunk in llm.stream(\"写一首关于春天的诗歌\"):\n",
    "    print(chunk, end=\"\", flush=False)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d3078e58317050d8"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "248ac8ae5c2ba88"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "f03b00090744bb71"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
